package rhinestone.Control;

import javax.naming.NameNotFoundException;
import rhinestone.Model.Product;
import rhinestone.Model.ProductContainer;
import rhinestone.Model.Machinery;
import rhinestone.Model.MachineryContainer;
import rhinestone.Model.Pack;
import rhinestone.Model.PackContainer;

/**
 * This class is responsible for product management:
 * adding new products to the system, finding particular product,
 * editing and deleting it.
 *
 * @authro Group 2
 * @version 2010.12.12
 */
public class ProductController {
	private ProductContainer productContainer;
	private MachineryContainer machineryContainer;
	private PackContainer packContainer;

	/**
	 * Constructor, which initializes the containers passed as parameters.
	 */
	public ProductController(ProductContainer productContainer, MachineryContainer machineryContainer, PackContainer packContainer) {
		this.productContainer = productContainer;
		this.machineryContainer = machineryContainer;
		this.packContainer = packContainer;
	}

	/**
	 * Is called by the UI when manager wants to add a new product in the system.
	 * Parameters not final. We will definitely need more information to register the product.
	 */
	public void addProduct(String barcode, String name, String description, double price, int amountInDIY, int amountInTimberyard, int minAmount, int maxAmount) {
		if(productContainer.getProduct(barcode) == null) {
			Product product = new Product(barcode, name, description, price, amountInDIY, amountInTimberyard, minAmount, maxAmount);
			productContainer.addProduct(product);
			System.out.println(" --- Product added ---");
		}
		else
			System.out.println(" --- Product not added. This bar code already exists. ---");
	}

	public void addMachinery(String barcode, String name, String description, double price, String location) {
		if(machineryContainer.getMachinery(barcode) == null) {
		Machinery machinery = new Machinery(barcode, name, description, price, location);
		machineryContainer.addMachinery(machinery);
		System.out.println(" --- Machinery added ---");
		}
		else
			System.out.println(" --- Machinery not added. This bar code already exists. ---");
	}

	/**
	 * Is called by the UI when manager wants to get particular product information.
	 * @param barcode The bar code of the product the manager wants to get.
	 * @return The requested product.
	 */
	public Product getProduct(String barcode) {
		return productContainer.getProduct(barcode);
	}

	public Machinery getMachinery(String barcode) {
		return machineryContainer.getMachinery(barcode);
	}

	public Product removeProduct(String barcode) {
		return productContainer.removeProduct(barcode);
	}

	public Machinery removeMachinery(String barcode) {
		return machineryContainer.removeMachinery(barcode);
	}

	/**
	 * Updates the Product object with provided data and adds it to the container.
	 *
	 * The params are self talking.
	 * @param machinery
	 * @param barcode
	 * @param name
	 * @param description
	 * @param price
	 * @param location
	 */
	public void updateProduct(Product product, String barcode, String name, String description, String price, String amountInDIY, String amountInTimberyard, String minAmount, String maxAmount) {
		if(!barcode.equals(""))
			product.setBarcode(barcode);
		if(!name.equals(""))
			product.setName(name);
		if(!description.equals(""))
			product.setDescription(description);
		if(!price.equals(Double.toString(product.getPrice()))) {
			try {
				product.setPrice(Double.parseDouble(price));
			} catch(NumberFormatException ex) {
				System.err.println("Product price NOT changed. Wrong price format: " + price);
			}
		}
		if(!amountInDIY.equals("")) {
			try {
				product.setAmountInDIY(Integer.parseInt(amountInDIY));
			} catch(NumberFormatException ex) {
				System.err.println("Product amount in DIY NOT changed. Wrong amount format: " + amountInDIY);
			}
		}
		if(!amountInTimberyard.equals("")) {
			try {
				product.setAmountInTimberyard(Integer.parseInt(amountInTimberyard));
			} catch(NumberFormatException ex) {
				System.err.println("Product amount in timberyard NOT changed. Wrong amount format: " + amountInTimberyard);
			}
		}
		if(!minAmount.equals("")) {
			try {
				product.setMinAmount(Integer.parseInt(minAmount));
			} catch(NumberFormatException ex) {
				System.err.println("Product minimal amount  NOT changed. Wrong amount format: " + minAmount);
			}
		}
		if(!maxAmount.equals("")) {
			try {
				product.setMaxAmount(Integer.parseInt(maxAmount));
			} catch(NumberFormatException ex) {
				System.err.println("Product maximal amount  NOT changed. Wrong amount format: " + maxAmount);
			}
		}

		productContainer.addProduct(product);
		System.out.println("Product updated.");
	}

	/**
	 * Updates the machinery object with provided data and adds it to the container.
	 *
	 * The params are self talking.
	 * @param machinery
	 * @param barcode
	 * @param name
	 * @param description
	 * @param price
	 * @param location
	 */
	public void updateMachinery(Machinery machinery, String barcode, String name, String description, String price, String location) {
		if(!barcode.equals(""))
			machinery.setBarcode(barcode);
		if(!name.equals(""))
			machinery.setName(name);
		if(!description.equals(""))
			machinery.setDescription(description);
		if(!price.equals("")) {
			try {
				machinery.setPrice(Double.parseDouble(price));
			} catch(NumberFormatException ex) {
				System.err.println("Machinery price NOT changed. Wrong price format: " + price);
			}
		}
		if(!location.equals(""))
			machinery.setLocation(location);

		machineryContainer.addMachinery(machinery);
		System.out.println("Machinery updated");
	}

	public void createPack(String barcode, String name, String description) {
		if(packContainer.getPack(barcode) == null) {
			Pack pack = new Pack(barcode, name, description);
			packContainer.addPack(pack);
			System.out.println(" --- Pack created ---");
		}
		else
			System.err.println("Pack with this bar code already exists.");
	}

	public Pack getPack(String barcode) {
		return packContainer.getPack(barcode);
	}

	public void addProductToPack(String packBarcode, Product product, int amount) throws NameNotFoundException {
		Pack pack = packContainer.getPack(packBarcode);
		if(pack == null)
			throw new NameNotFoundException();
		pack.addProduct(product, amount);
	}

	public void removeProductFromPack(String packBarcode, Product product, int amount) throws NameNotFoundException {
		Pack pack = packContainer.getPack(packBarcode);
		if(pack == null)
			throw new NameNotFoundException();
		pack.removeProduct(product, amount);
	}

	public Pack removePack(String barcode) {
		return packContainer.removePack(barcode);
	}
}
